package org.particlethink.context;

public final class DefaultContext implements Context {

    private final Context parent;
    private final String key;
    private final Object value;

    private DefaultContext(Context parent, String key, Object value) {
        this.parent = parent;
        this.key = key;
        this.value = value;
    }

    @Override
    public Context withValue(String key, Object value) {
        return new DefaultContext(this, key, value);
    }

    @Override
    public Object getValue(String key) {
        if (this.key != null && this.key.equals(key)) {
            return this.value;
        } else if (this.parent != null) {
            return this.parent.getValue(key);
        }
        return null;
    }

    public static class Builder {
        public static Context background() {
            return new DefaultContext(null, null, null);
        }

        public static Context withValue(String key, Object value) {
            return new DefaultContext(null, key, value);
        }
    }
}
